<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${import.dao.packageName}.${import.dao.className}" >
<cache />
<!-- 查询字段 -->
<sql id="columnList" >
	<#list fields as field>${alias}.${field.column.name},</#list>${alias}.id
</sql>

<!-- 获取一个映射抽象 -->
<sql id="maps">
    select
    <if test="condition.appendConditions!=null">
        <include refid="columnList"/>,
        ${'$'}{condition.appendConditions}
    </if>
    <if test="condition.mapConditions!=null">
        ${'$'}{condition.mapConditions}
    </if>
    <if test="condition.appendConditions==null and condition.mapConditions==null">
        <include refid="columnList"/>
    </if>
    from ${tableName} ${alias}
    <if test="condition.leftConditions!=null">${'$'}{condition.leftConditions}</if>
    <include refid="whereClause"/>
    <if test="orderByClause!=null">${'$'}{orderByClause}</if>
</sql>

<!--通过id集合in查询-->
<sql id="inClause">
    <if test="column!=null">
        <if test="ids==null">
            and 1=0
        </if>
        <if test="ids!=null">
            <if test="ids.size>0">
                and  b.${'$'}{column}
                <foreach item="items" index="index" collection="ids" open="(" separator="," close=")">
                    ${'#'}{items}
                </foreach>
            </if>
            <if test="ids.size==0">
                and 1=0
            </if>
        </if>
    </if>
</sql>

<!-- 查询条件 -->
<sql id="whereClause">
    where 1=1
    <trim  suffixOverrides="," >
        <if test="condition.id != null" >and ${alias}.id = ${'#'}{condition.id}</if>
        <#list fields as field>
        <if test="condition.${field.name} != null" >and  ${alias}.${field.column.name} = ${'#'}{condition.${field.name}}</if>
        </#list>
    </trim>
    <include refid="inClause"/>
</sql>

<!-- 插入记录 -->
<insert id="add"  >
    insert into ${tableName} (id,<#list fields as field>${field.column.name}<#if field_has_next>,</#if></#list>)
    values(${'#'}{id,jdbcType=BIGINT},<#list fields as field>${'#'}{${field.name}}<#if field_has_next>,</#if></#list>)
</insert>


<!-- 修改记录，只修改只不为空的字段 -->
<update id="update" >
    update ${tableName} set
    <trim  suffixOverrides="," >
        <#list fields as field>
        <if test="${field.name} != null ">${field.column.name}=${'#'}{${field.name}},</if>
        </#list>
        <if test="clear!=null ">${'$'}{clear} = null ,</if>
    </trim>
    where id = ${'#'}{id}<#list fields as field><#if field.name=='version'> and version = ${'#'}{version}-1</#if></#list>
</update>


<!-- 删除记录 -->
<delete id="del">
    delete from ${tableName} where id = ${'#'}{id}
</delete>

<!-- 根据id查询  -->
<select id="get" resultType="${import.entity.packageName}.${import.entity.className}" parameterType="java.lang.Long">
    select
    <include refid="columnList"/>
    from ${tableName} ${alias}
    where id = ${'#'}{id}
</select>

<!-- 列表总数-->
<select id="count" resultType="java.lang.Long">
    select count(1)
    from ${tableName} ${alias}
    <include refid="whereClause"/>
</select>

<!-- 查询page -->
<select id="findPage" resultType="${import.entity.packageName}.${import.entity.className}">
    select
    <include refid="columnList"/>
    from ${tableName} ${alias}
    <include refid="whereClause"/>
    <if test="orderByClause!=null">${'$'}{orderByClause}</if>
</select>


<!-- 查询列表list -->
<select id="findList" resultType="${import.entity.packageName}.${import.entity.className}">
    select
    <include refid="columnList"/>
    from ${tableName} ${alias}
    <include refid="whereClause"/>
    <if test="orderByClause!=null">${'$'}{orderByClause}</if>
</select>


<!-- 查询符合条件的数据的id集合 -->
<select id="findIds" resultType="java.lang.Long">
    select ${alias}.${'$'}{idsColumn}
    from ${tableName} ${alias}
    <include refid="whereClause"/>
    <if test="orderByClause!=null">${'$'}{orderByClause}</if>
</select>

<!-- 获取一个对象通过非id -->
<select id="one" resultType="${import.entity.packageName}.${import.entity.className}">
    select
    <include refid="columnList"/>
    from ${tableName} ${alias}
    <include refid="whereClause"/>
    <if test="orderByClause!=null">${'$'}{orderByClause}</if>
    limit 0,1
</select>

<!-- 获取一个映射集合 -->
<select id="mapList" resultType="java.util.Map">
    <include refid="maps"></include>
</select>

<!-- 获取一个映射分页 -->
<select id="mapPage" resultType="java.util.Map">
    <include refid="maps"></include>
</select>

<!-- 获取一个映射 -->
<select id="mapOne" resultType="java.util.Map">
    <include refid="maps"></include>
    limit 0,1
</select>
</mapper>
